package com.xy.p2p.action.front;



import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Ttenderrecord;
import com.xy.p2p.model.TtenderrecordForTransfer;
import com.xy.p2p.model.Ttransferinfo;
import com.xy.p2p.model.Ttransferrecord;
import com.xy.p2p.service.TtenderrecordService;
import com.xy.p2p.service.TtransferinfoService;



@Controller
public class TtransferinfoAction{
	private static final Logger log = LoggerFactory.getLogger(TtransferinfoAction.class);
	public TtransferinfoAction() {
	}
 

	/**
	 * 转让标的信息表查询
	 * 
	 * @param ttransferinfo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/v_ttransferinfoList.jspx")
	public String list(HttpServletRequest request, ModelMap model) {
		Ttransferinfo ttransferinfo = new Ttransferinfo();
		Tmember tmember = (Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY);
		ttransferinfo.setTmember(tmember);
		List<Ttransferinfo> list = ttransferinfoService.getTtransferinfo(toMap(ttransferinfo));
		model.addAttribute("list", list);
        setDict(model);
		return "/front/member/transferinfoListQuery.html";
	}

	
	
 	/**
	 * 转让标的信息表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/v_ttransferinfoAdd.jspx")
	public String add(Long trid, HttpServletRequest request, ModelMap model) {
		Ttenderrecord t = ttenderrecordService.get(trid);
		TtenderrecordForTransfer tt = new TtenderrecordForTransfer(t);
		//判断投标90日后并且计息金额大于50,重新回到债权转让申请列表
		if(tt.getSubDay()-90<0 || tt.getMibmoney()<50)
			return "redirect:/member/v_ttenderrecordList1.jspx";
		Ttransferinfo ttransferinfo = new Ttransferinfo();
		Ttenderrecord ttenderrecord = ttenderrecordService.get(trid);
		ttransferinfo.setTtenderrecord(ttenderrecord);
		ttransferinfo.setTmember((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		model.addAttribute("ttransferinfo", ttransferinfo);
		//获取当前可转让的金额(债权总金额-已转让的金额)
		model.addAttribute("enable", ttransferinfo.getTtenderrecord().getTdmoney()-ttransferinfoService.getTotalTransferMoney(ttenderrecord));
		setDict(model);
		return "/front/member/transferinfoAdd.html";
	}

	/**
	 * 转让标的信息表新增保存
	 * 
	 * @param ttransferinfo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/o_ttransferinfoSave.jspx")
	public String save(Ttransferinfo ttransferinfo, HttpServletRequest request, ModelMap model) {
//		WebErrors errors = validateSave(ttransferinfo, request);
//		if (errors.hasErrors()) {
//			return errors.showErrorPage(model);
//		}
		Long trid = Long.parseLong(request.getParameter("trid"));
		Ttenderrecord ttenderrecord = ttenderrecordService.get(trid);
		ttransferinfo.setTtenderrecord(ttenderrecord);
		ttransferinfo.setTmember((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		//新增
		ttransferinfo.setTrfactor(ttransferinfo.getTrfactor()/100.00);
		ttransferinfo = ttransferinfoService.save(ttransferinfo);
		if(null==ttransferinfo)
			return "error";//错误页面,转让金额必须为50的倍数并且不能大于可转让金额
		return "redirect:/member/tomembercenter.jspx";
	}

	//显示可购买的转让标的
	@RequestMapping("/member/v_ttransferinfoBuyableList.jspx")
	public String buyableList(HttpServletRequest request, ModelMap model) {
		List<Ttransferinfo> list = ttransferinfoService.getBuyableList();
		model.addAttribute("list", list);
        setDict(model);
		return "/front/businessPage/transferinfoList.html";
	}

	//点击购买债权, 查询债权详细
	@RequestMapping("/member/v_ttransferinfoDetail.jspx")
	public String detail(long tiid, HttpServletRequest request, ModelMap model) {
		Ttransferinfo ttransferinfo = ttransferinfoService.get(tiid);
//		ttransferinfo.setTrprice();
		model.addAttribute("ttransferinfo", ttransferinfo);
        setDict(model);
		return "/front/businessPage/transferinfoeBuyying.html";
	}
	

	
	
 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param ttransferinfo
	 * @return
	 */	private Map<String,Object> toMap(Ttransferinfo ttransferinfo){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(ttransferinfo.getTiid()!=null&&!ttransferinfo.getTiid().equals("")){
			map.put("tiid", ttransferinfo.getTiid());
		}
		if(ttransferinfo.getTtenderrecord()!=null){
			map.put("Ttenderrecord", ttransferinfo.getTtenderrecord());
		}
		if(ttransferinfo.getTmember()!=null){
			map.put("Tmember", ttransferinfo.getTmember());
		}
		if(ttransferinfo.getTrmoney()!=null&&!ttransferinfo.getTrmoney().equals("")){
			map.put("trmoney", ttransferinfo.getTrmoney());
		}
		if(ttransferinfo.getTrfactor()!=null&&!ttransferinfo.getTrfactor().equals("")){
			map.put("trfactor", ttransferinfo.getTrfactor());
		}
		if(ttransferinfo.getTrreason()!=null&&!ttransferinfo.getTrreason().equals("")){
			map.put("trreason", ttransferinfo.getTrreason());
		}
		if(ttransferinfo.getTrprice()!=null&&!ttransferinfo.getTrprice().equals("")){
			map.put("trprice", ttransferinfo.getTrprice());
		}
		if(ttransferinfo.getStatus()!=null&&!ttransferinfo.getStatus().equals("")){
			map.put("status", ttransferinfo.getStatus());
		}
		if(ttransferinfo.getIstransfer()!=null&&!ttransferinfo.getIstransfer().equals("")){
			map.put("istransfer", ttransferinfo.getIstransfer());
		}
		if(ttransferinfo.getTsfmoney()!=null&&!ttransferinfo.getTsfmoney().equals("")){
			map.put("tsfmoney", ttransferinfo.getTsfmoney());
		}     
		if(ttransferinfo.getTrdatetime()!=null&&!ttransferinfo.getTrdatetime().equals("")){
			map.put("trdatetime", ttransferinfo.getTrdatetime());
		}       
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TTRANSFERINFO_STATUS));
      model.addAttribute("istransferMap", DictUtils.getDictByType(DictUtils.TTRANSFERINFO_ISTRANSFER));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Ttransferinfo ttransferinfo, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TtransferinfoService ttransferinfoService;  
	@Autowired
	private TtenderrecordService ttenderrecordService;
}